package com.daqian.mall.controller;

import com.daqian.mall.entity.ModuleGoodsClassify;
import com.daqian.mall.rest.RestResult;
import com.daqian.mall.service.ClassifyService;
import com.daqian.mall.service.ClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@RestController
public class ClassifyController {

    @Autowired
    private ClassifyService classifyService;

    /**
     * 根据id获取详情
     *
     * @param id
     * @return
     */
    @GetMapping("/module/mall/classify/{id}")
    public RestResult getById(@PathVariable("id") Long id) {
        return RestResult.success(classifyService.getById(id));
    }

    /**
     * 获取所有分类
     *
     * @return
     */
    @GetMapping("/module/mall/classify/all")
    public RestResult listAllClassify() {
        return RestResult.success(classifyService.listAllClassify());
    }

    /**
     * 新增分类
     *
     * @return
     */
    @PostMapping("/module/mall/classify")
    public RestResult saveClassify(ModuleGoodsClassify entity) {
        Assert.notNull(entity.getName(), "分类名不可为空");
        classifyService.saveClassify(entity);
        return RestResult.success();
    }

    /**
     * 修改分类
     *
     * @return
     */
    @PutMapping("/module/mall/classify/{id}")
    public RestResult updateClassify(@PathVariable("id") Long id, @RequestBody ModuleGoodsClassify entity) {
        Assert.notNull(id, "id不可为空");
        entity.setId(id);
        classifyService.updateClassify(entity);
        return RestResult.success();
    }

    /**
     * 删除单个分类
     *
     * @return
     */
    @DeleteMapping("/module/mall/classify/{id}")
    public RestResult deleteClassify(@PathVariable("id") Long id) {
        Assert.notNull(id, "id不可为空");
        classifyService.deleteClassify(id);
        return RestResult.success();
    }

    /**
     * 删除多个分类
     *
     * @return
     */
    @DeleteMapping("/module/mall/classify")
    public RestResult deleteClassify(@RequestParam("ids[]") Long[] ids) {
        Assert.notNull(ids, "id不可为空");
        classifyService.deleteClassify(ids);
        return RestResult.success();
    }
}
